package service.data.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class RemoteCallUtil {

    public String get(RestTemplate restTemplate,String url){
        System.out.println(url);
        Map<String,Object> item = new HashMap<>();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept","application/json;charset=utf-8");

        HttpEntity httpEntity = new HttpEntity(item,headers);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        int statusCodeValue = exchange.getStatusCodeValue();
        String body = exchange.getBody();
        return body;
    }

    public String post(RestTemplate restTemplate,String url,Map<String,Object> item){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept","application/json;charset=utf-8");

        HttpEntity httpEntity = new HttpEntity(item,headers);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        int statusCodeValue = exchange.getStatusCodeValue();
        String body = exchange.getBody();
        return body;
    }

    public String put(RestTemplate restTemplate,String url){
        Map<String,Object> item = new HashMap<>();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept","application/json;charset=utf-8");

        HttpEntity httpEntity = new HttpEntity(item,headers);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, String.class);
        int statusCodeValue = exchange.getStatusCodeValue();
        String body = exchange.getBody();
        return body;
    }

    public String delete(RestTemplate restTemplate,String url){
        Map<String,Object> item = new HashMap<>();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept","application/json;charset=utf-8");

        HttpEntity httpEntity = new HttpEntity(item,headers);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, String.class);
        int statusCodeValue = exchange.getStatusCodeValue();
        String body = exchange.getBody();
        return body;
    }
}
